﻿using System.Linq.Expressions;
using SimpleOrmCFPC.Core.Query.Requests;
using SimpleOrmCFPC.Core.Query.Where;
using SimpleOrmCFPC.Core.Query.expressions.ExprssionCompilers.Helpers;

namespace SimpleOrmCFPC.Core.Query.expressions.RequestBuilder.WhereElmentsBuilder.Helpers
{
    public class BinaryExpressionWhereElementBuilder : IWhereElementBuilder
    {
        private AnyWhereElementFromExpressionComputer expressionComputer;

        public BinaryExpressionWhereElementBuilder(AnyWhereElementFromExpressionComputer expressionComputer)
        {
            this.expressionComputer = expressionComputer;
        }

        public IWhereElement Compute(Expression expression)
        {
            var bin = (BinaryExpression)expression;

            var leftr = expressionComputer.Compute(bin.Left);
            var rightr = expressionComputer.Compute(bin.Right);
            return new OperatorWhereElement(bin.NodeType.ConvertToSqlOperator(), leftr, rightr);
        }

        public void FillAdditionalInformation(Expression expression, SQLRequest request)
        {
            var bin = (BinaryExpression)expression;
            expressionComputer.FillAdditionalInformation(bin.Left, request);
            expressionComputer.FillAdditionalInformation(bin.Right, request);
        }
    }
}